<?php

defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'detail';
if($_RQ['op']=='detail'){
	/*******main*******/
	$return['ischecklogin']=cfc('core')->checklogin('','',true);
	$id = intval($_RQ['id']);
	if(!$id){
		exi('该活动不存在!','error');
	}
	$this->session('gameid',$id);
	$team=pdo_get('jy_weishop_game',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$day=date('Y/m/d');
	$openlog=$this->model('model')->upopenlog($team);
	$this->session('openlogid',$openlog['id']);
	$team['pluginset']=json_decode($team['pluginset'],true);
    if($team['pluginset']['opentime']){
        $opentime=strtotime($team['pluginset']['opentime']);
        if($opentime>time()){
            $team['status']=0;
        }
    }
	$team['reward']=json_decode($team['reward'],true);
	foreach($team['reward'] as $key=>&$rv){
		$rv['isjifen']=pdo_getsum('jy_manghe_feelog',array('rewardkey'=>$key,'openlogid'=>$openlog['id'],'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'gameid'=>$team['id']),'fee');
		$rv['isjifen']=$rv['isjifen']?:0;
	}
	$team['createtime']=date('Y/m/d H:i:s',$team['createtime']);
	$team['salenum']=pdo_count('jy_manghe_feelog',array('uniacid'=>$_SESSION['uniacid'],'gameid'=>$team['id']),'id');
	$team['isfee']=pdo_getsum('jy_manghe_feelog',array('uniacid'=>$_SESSION['uniacid'],'gameid'=>$team['id']),'fee');
	$team['statustitle']=$this->goods_status[$team['status']];
	if($team['status']==1 && $openlog){
		$rest_time=$openlog['opentime']-TIMESTAMP;
		if($rest_time>0){
			$team['rest_time']=model('jy_weishop/model')->date($rest_time);
		}
	}
	if(!$openlog){
        $team['statustitle']='活动还未开始';
        $team['status']=0;
    }
	$return['team']=$team;
	$address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'','isdefault desc');
	$address['pcd']=json_decode($address['pcd'],true);
	$return['post']=array('gameid'=>$team['id'],'sendtype'=>$sendtype,'address'=>$address);
	$return['params']=array('status'=>0,'gameid'=>$team['id']);
	$return['sharedata']=array(
			'title'=>$nickname.'-发起盲盒活动啦！参与就可获利了！',
			'desc'=>'参与盲盒活动啦，参与就可获利了！',
			'link'=>custom_url('jy_manghe/detail//id='.$team['id'].'&fromuser='.$_SESSION['uid'],true),
			'imgUrl'=>tomedia($team['image'])
	);
	$return['btitle']=$this->btitle;
    $return['showcanlog']=0;
    $tips=$return['pluginset']['tips']?:'最小投入积分单位:'.$team['pluginset']['he_onejifen'].',最大投入积分单位:'.$team['pluginset']['he_maxjifen'].',最多投'.$team['pluginset']['he_onenum'].'个盲盒!';
    $return['tips']=$tips;
    $return['sale_red']=$this->pdo_getall('maxlog',array('type'=>0,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
    foreach($return['sale_red'] as &$sv){
        $sv['member']=cfc('core')->member_show($sv['mid']);
        $sv['createtime']=date('Y-m-d H:i',$sv['createtime']);
    }
    $return['red']=$_SESSION['member'];
    $return['red']['log']=$this->pdo_get('maxlog',array('status'=>0,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
    $credittype=$this->pdo_getcolumn('maxlog',array('type'=>0,'uniacid'=>$_SESSION['uniacid']),'credittype');
    $return['red']['typetitle']=$this->prizeType[$credittype];
    $return['red']['allfee']=$this->pdo_getsum('maxlog',array('type'=>0,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'fee');
    $return['red']['red_mp3']=$return['pluginset']['red_mp3'];
    if($return['red']['log']['id']){
        $return['showredpage']=true;
        $this->pdo_update('maxlog',array('status'=>1),array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
    }
    if(!$_SESSION['show_xieyi']){
        $return['show_xieyi']=1;
        $_SESSION['show_xieyi']=1;
    }
	set_html('title','盲盒详情');
	exi($return);	
}
if($_RQ['op']=='getlist'){
	$team=pdo_get('jy_weishop_game',array('uniacid'=>$_SESSION['uniacid'],'id'=>$this->session('gameid')));
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'gameid'=>$this->session('gameid'));
	if($_RQ['status']==3){
		$params['status']=array(3,4,6,7);
	}elseif($_RQ['status']==1){
		$params['status']=array(2,5);
	}else{
		$params['status']=array(0,1);
	}
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_manghe_feelog',$params,array($pindex,$psize),$total,$fields,'',$orderby);	
	foreach($return['list'] as &$v){
		$v['statustitle']=$this->canlog_status[$v['status']];
		$v['createtime']=date('Y-m-d H:i:s',$v['createtime']);
		$v['member']=pdo_get('core_members',array('id'=>$v['mid']),array('nickname','avatar'));
		$v['member']['nickname']=$v['member']['nickname']?cfc('tools')->substr($v['member']['nickname'],0,1).'****':'******';
        $v['opentime']=pdo_getcolumn('jy_manghe_openlog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$v['openlogid']),'opentime');
		$v['day']=date('Y-m-d',$v['opentime']);
		$v['dayno']=pdo_getcolumn('jy_manghe_openlog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$v['openlogid']),'dayno');
	}
	exi($return);
}
// 完成下单
if($_RQ['op']=='post') {
    $rewards = get_postdata();
    $gameid = $this->session('gameid');
    $alljifen = $_RQ['alljifen'] ?: 0;
    if (!$alljifen) {
        foreach ($rewards as $rv) {
            if ($rv['addjifen']) {
                $alljifen += $rv['addjifen'];
            }
        }
    }else{
        $isjian=1;
        $isxuan=0;
        foreach ($rewards as $rv) {
            if($rv['isxuan']){
                $isxuan++;
            }
        }
        if($isxuan<=0){
            exi('请选择要开的盲盒！','error');
        }
        $oneaddjifen=$alljifen/$isxuan;
        foreach ($rewards as &$rrv) {
            if($rrv['isxuan']){
                $rrv['addjifen'] = $oneaddjifen;
            }
        }
    }
	if($alljifen<=0){
		exi('请输入要投入的积分额度！','error');
	}
	$game=pdo_get('jy_weishop_game',array('uniacid'=>$_SESSION['uniacid'],'id'=>$gameid));
	$member=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','credit3','credit4','credit6','mobile','avatar','nickname'));
    if($member['credit1']<$alljifen){
        exi('您的积分不足！当前剩余'.$member['credit1'],'error');
    }
    foreach($rewards as $key=>$rv) {
        if ($rv['addjifen']) {
            $rv['key'] = $key;
            $this->model('model')->check_buy($game, $rv, $member);
        }
    }
    model('core/member')->member_credit($_SESSION['uid'],'credit1',$alljifen,false,'jy_guashop','拆盲盒消耗');
    foreach($rewards as $key=>$rv) {
        if ($rv['addjifen']) {
            $log = array(
                'gameid' => $gameid,
                'openlogid' => $this->session('openlogid'),
                'fee' => $rv['addjifen'],
                'isadd' => 1,
                'rewardkey' => $key,
            );
            $feelogid = $this->model('model')->add_feelog($log);
        }
    }
	exi('success');
}
?>